package MyUtil;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCUtils {
    //数据库url、用户名和密码
    private static String driverName;
    private static String url;
    private static String userName;
    private static String password;

    static {
        Properties properties = new Properties();
        try(InputStream is = new FileInputStream("src/main/resources/db.properties");) {
            properties.load(is);
            driverName = properties.getProperty("driverName");
            url = properties.getProperty("url");
            userName = properties.getProperty("userName");
            password = properties.getProperty("password");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getMyConnection() throws Exception{
        Class.forName(driverName);
        Connection conn = null;
        conn = DriverManager.getConnection(url, userName, password);
        return conn;
    }

    public static void release(Connection conn, PreparedStatement preparedStatement, ResultSet resultSet) throws Exception {
        if (conn!=null){
            conn.close();
        }
        if (preparedStatement!=null){
            preparedStatement.close();
        }
        if (resultSet!=null){
            resultSet.close();
        }
    }
}
